package org.ourgrid.broker.business.schedulerdata.executor;

import java.util.ArrayList;
import java.util.List;

import org.ourgrid.broker.BrokerConstants;
import org.ourgrid.broker.business.schedulerdata.SchedulerData;
import org.ourgrid.common.interfaces.Worker;
import org.ourgrid.common.internal.IResponseTO;
import org.ourgrid.common.internal.response.RegisterInterestResponseTO;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class RegisterInterestOnWorkerExecutor extends AbstractSchedulerDataExecutor {
	
	/**
	 * <REGISTER_INTEREST_ON_WORKER workerID='String'/>
	 */
	public List<IResponseTO> execute(SchedulerData command) {
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
		
		Document logDocFile = super.getXMl(command.getXml());
		
		Element element = logDocFile.getDocumentElement();
		
		String wID = element.getAttribute("workerID");
		
		if (wID != null) {
			RegisterInterestResponseTO registerInterestResponse = new RegisterInterestResponseTO();
			registerInterestResponse.setMonitorableAddress(wID);
			registerInterestResponse.setMonitorableType(Worker.class);
			registerInterestResponse.setMonitorName(BrokerConstants.LOCAL_WORKER_PROVIDER_CLIENT);
			registerInterestResponse.setDetectionTime(30);
			registerInterestResponse.setHeartbeatDelay(10);
			
			responses.add(registerInterestResponse);
		}	
		
		return responses;
	}
}
